package com.hmys.api.service.impl;

import com.hmys.api.mapper.WebUserMapper;
import com.hmys.api.mapper.WebUserRoleMapper;
import com.hmys.api.model.*;
import com.hmys.api.service.WebUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by chen365 on 2018/03/14.
 */
@Service
public class WebUserServiceImpl implements WebUserService {
    @Autowired
    private WebUserMapper webUserMapper;

    @Autowired
    private WebUserRoleMapper webUserRoleMapper;

    @Override
    public WebUser selectByPhone(String phone) {
        return webUserMapper.selectByPhone(phone);
    }

    @Override
    public WebUser getCurrentUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Principal principal = (Principal) authentication.getPrincipal();
            return principal.getUser();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean isExist(String phone) {
        return this.selectByPhone(phone) == null ? false : true;
    }

    @Override
    @Transactional
    public int create(WebUser webUser) {
        webUser.setCreateTime(new Date());
        webUserMapper.insertSelective(webUser);
        Long userId = webUser.getUserId();
        if (userId == null || userId < 1) {
            return 0;
        }

        System.err.println(webUser.toString());
        WebUserRole webUserRole = new WebUserRole();
        webUserRole.setUserId(userId);
        webUserRole.setRoleId(3L); //3	ROLE_USER	用户
        System.err.println(webUserRole.toString());
        webUserRoleMapper.insertSelective(webUserRole);

        return 1;

    }

    @Override
    @Transactional
    public WebUser sginUp(WebUser webUser) {
        webUser.setCreateTime(new Date());
        webUserMapper.insertSelective(webUser);
        Long userId = webUser.getUserId();

        System.err.println(webUser.toString());
        WebUserRole webUserRole = new WebUserRole();
        webUserRole.setUserId(userId);
        webUserRole.setRoleId(3L); //3	ROLE_USER	用户
        System.err.println(webUserRole.toString());
        webUserRoleMapper.insertSelective(webUserRole);

        System.err.println("webUser:"+webUser.toString());

        return webUser;

    }

    @Override
    public WebUserBasics selectBasicsByPrimaryKey(Long userId) {
        return webUserMapper.selectBasicsByPrimaryKey(userId);
    }

    @Override
    public WebUserInvest selectInvestByPrimaryKey(Long userId) {
        return webUserMapper.selectInvestByPrimaryKey(userId);
    }

    @Override
    public int updateInvestByPrimaryKey(WebUserInvest record) {
        return webUserMapper.updateInvestByPrimaryKey(record);
    }

    @Override
    public int updateBasicsByPrimaryKey(WebUserBasics record) {
        return webUserMapper.updateBasicsByPrimaryKey(record);
    }

    @Override
    public String selectPhotoByPrimaryKey(Long userId) {
        return webUserMapper.selectPhotoByPrimaryKey(userId);
    }

    @Override
    public int updatePhotoByPrimaryKey(WebUser record) {
        return webUserMapper.updatePhotoByPrimaryKey(record);
    }
}
